<template>
  <view class="container">
    <view class="header">
      <text class="title">Vireo商城</text>
      <text class="subtitle">欢迎来到Vireo商城，享受优质购物体验</text>
    </view>
    
    <view class="banner">
      <image class="banner-image" src="/static/banner.jpg" mode="aspectFill"></image>
    </view>
    
    <view class="categories" v-if="categories.length > 0">
      <view class="category-item" v-for="(category, index) in categories" :key="index" @tap="navigateToCategory(category.id)">
        <image class="category-icon" :src="category.metadata?.icon || '/static/category-digital.png'" mode="aspectFit"></image>
        <text class="category-name">{{ category.name }}</text>
      </view>
    </view>
    
    <view class="products" v-if="products.length > 0">
      <view class="section-title">热门商品</view>
      <view class="product-list">
        <view class="product-item" v-for="(product, index) in products" :key="index" @tap="navigateToProduct(product.id)">
          <image class="product-image" :src="product.thumbnail || '/static/product-phone.jpg'" mode="aspectFill"></image>
          <text class="product-name">{{ product.title }}</text>
          <text class="product-price">¥{{ product.variants?.[0]?.prices?.[0]?.amount || 0 }}</text>
        </view>
      </view>
    </view>
    
    <view class="loading" v-if="loading">
      <text>加载中...</text>
    </view>
  </view>
</template>

<script>
import api from '@/utils/api.js';

export default {
  data() {
    return {
      categories: [],
      products: [],
      loading: false
    };
  },
  onLoad() {
    // 检查登录状态
    this.checkLoginStatus();
    // 加载数据
    this.loadData();
  },
  methods: {
    // 检查登录状态
    checkLoginStatus() {
      const token = uni.getStorageSync('token');
      if (!token) {
        console.log('用户未登录');
      } else {
        console.log('用户已登录');
      }
    },
    
    // 加载数据
    async loadData() {
      this.loading = true;
      try {
        // 并行加载分类和商品数据
        const [categoriesRes, productsRes] = await Promise.all([
          this.loadCategories(),
          this.loadProducts()
        ]);
        
        if (categoriesRes) {
          this.categories = categoriesRes.product_categories || [];
        }
        
        if (productsRes) {
          this.products = productsRes.products || [];
        }
      } catch (error) {
        console.error('加载数据失败:', error);
        uni.showToast({
          title: '加载数据失败',
          icon: 'none'
        });
      } finally {
        this.loading = false;
      }
    },
    
    // 加载分类数据
    async loadCategories() {
      try {
        const res = await api.getCategories();
        return res;
      } catch (error) {
        console.error('加载分类失败:', error);
        return null;
      }
    },
    
    // 加载商品数据
    async loadProducts() {
      try {
        const res = await api.getProducts({ limit: 8 });
        return res;
      } catch (error) {
        console.error('加载商品失败:', error);
        return null;
      }
    },
    
    // 跳转到分类页面
    navigateToCategory(categoryId) {
      uni.showToast({
        title: `跳转到分类 ${categoryId}`,
        icon: 'none'
      });
    },
    
    // 跳转到商品详情页
    navigateToProduct(productId) {
      uni.showToast({
        title: `跳转到商品 ${productId}`,
        icon: 'none'
      });
    }
  }
};
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background-color: #f8f8f8;
}

.header {
  padding: 40rpx 30rpx;
  background-color: #fff;
  text-align: center;
  
  .title {
    font-size: 48rpx;
    font-weight: bold;
    color: #333;
    display: block;
    margin-bottom: 10rpx;
  }
  
  .subtitle {
    font-size: 28rpx;
    color: #666;
  }
}

.banner {
  height: 300rpx;
  margin: 20rpx 30rpx;
  border-radius: 20rpx;
  overflow: hidden;
  
  .banner-image {
    width: 100%;
    height: 100%;
  }
}

.categories {
  display: flex;
  justify-content: space-around;
  padding: 30rpx;
  background-color: #fff;
  margin: 0 30rpx 20rpx;
  border-radius: 20rpx;
  
  .category-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    
    .category-icon {
      width: 80rpx;
      height: 80rpx;
      margin-bottom: 10rpx;
    }
    
    .category-name {
      font-size: 24rpx;
      color: #333;
    }
  }
}

.products {
  padding: 0 30rpx 30rpx;
  
  .section-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
  }
  
  .product-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    
    .product-item {
      width: 48%;
      background-color: #fff;
      border-radius: 20rpx;
      padding: 20rpx;
      margin-bottom: 20rpx;
      
      .product-image {
        width: 100%;
        height: 200rpx;
        border-radius: 10rpx;
        margin-bottom: 10rpx;
      }
      
      .product-name {
        font-size: 28rpx;
        color: #333;
        display: block;
        margin-bottom: 10rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      
      .product-price {
        font-size: 32rpx;
        color: #ff4d4f;
        font-weight: bold;
      }
    }
  }
}

.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40rpx;
  
  text {
    font-size: 28rpx;
    color: #999;
  }
}
</style>
